@using BootBlazor.Servers.Data
@using BootBlazor.Servers.Resources
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.EntityFrameworkCore
@using Microsoft.Extensions.Localization
@using MudBlazor

@inject IDbContextFactory<BootBlazorDbContext> _dbFactory;
@inject IStringLocalizer<CommonComponent> _loc;

<div class="d-flex flex-column">
    <MudTextField Value="SelectedMenuName"  Variant="Variant.Outlined"
                  Margin="Margin.Dense" @onfocus="ElementFocused" @onblur="MouseBlur" Label="@(Label ?? _loc["OrganizationTreeSelect_Title"])"
                  Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Clear"
                  OnAdornmentClick="CleanSelected" IconSize="Size.Small" Class="search-com" />
    <MudPopover Open="@_popoverOpen" Fixed="true" Class="pa-4" tabindex="-1" Style="min-width:300px;"
                RelativeWidth=DropdownWidth.Relative @onmouseenter="MouseEnterPopover" @onmouseleave="MouseLeavePopover" @onblur="MouseBlur">
        <div class="d-flex align-center">

            <MudText Typo="Typo.subtitle1">
                @_loc["OrganizationTreeSelect_TitleText"]
            </MudText>
            <MudSpacer />
        </div>
        @if (OrganizationItems.Count > 0)
        {
            <MudDivider />
        }
        <div style="max-height:400px;overflow:auto;">
            <MudTreeView T="OrganizationItem" Items="@OrganizationItems" Hover="true" SelectedValue="SelectedTreeItem"
                         SelectedValueChanged="SelectedTreeItemChanged" SelectionMode="SelectionMode.SingleSelection">
                <ItemTemplate>
                    <MudTreeViewItem @bind-Expanded="@context.Expanded" Items="@context.Children" Value="context.Value">
                        <Content>
                            <MudTreeViewItemToggleButton @bind-Expanded="@context.Expanded" Visible="context.HasChildren" />
                            @if (ShowIcon)
                            {
                                <MudIcon Icon="@Icons.Material.Outlined.AccountTree" Class="mr-2"></MudIcon>
                            }
                            <MudText Typo="Typo.subtitle2" Style="user-select:none;">@context.Value?.Name</MudText>
                            <MudSpacer />
                        </Content>
                    </MudTreeViewItem>
                </ItemTemplate>
            </MudTreeView>
        </div>
    </MudPopover>
</div>
